﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Xml;
using System;

public class xmlLoader : MonoBehaviour {

    //SingleTon
    private static xmlLoader instance;
    private static GameObject container;

    public static xmlLoader GetInstance()
    {
        if (!instance)
        {
            container = new GameObject();
            container.name = "xmlLoader";
            instance = container.AddComponent(typeof(xmlLoader)) as xmlLoader;
        }
        return instance;
    }

    public List<Dictionary<string, string>> m_XML_UnitSpeech = new List<Dictionary<string, string>>();
    public List<Dictionary<string, string>> m_XML_Buildings = new List<Dictionary<string, string>>();
    public List<Dictionary<string, string>> m_XML_Weapons = new List<Dictionary<string, string>>();
    public List<Dictionary<string, string>> m_XML_Traits = new List<Dictionary<string, string>>();
    public List<Dictionary<string, string>> m_XML_Events = new List<Dictionary<string, string>>();
    public List<Dictionary<string, string>> m_XML_EventLogs = new List<Dictionary<string, string>>();

    public List<Dictionary<string, string>> m_XML_Defs = new List<Dictionary<string, string>>();

    

	// Use this for initialization
	void Start () {
	}

    public void Load_Xmls()
    {
        XML_Load_UnitSpeech();

        XML_Load_ETC("Buildings");
        XML_Load_ETC("Weapons");
        XML_Load_ETC("Traits");
        XML_Load_ETC("Events");
        XML_Load_ETC("EventDialog");
        //Enum순서대로 로드할것!
        XML_Load_Defs("Def/SystemMsgDef");
        XML_Load_Defs("Def/PerkDef");
        XML_Load_Defs("Def/FirstNameDef");
        XML_Load_Defs("Def/LastNameDef");
        XML_Load_Defs("Def/StatDef");
        XML_Load_Defs("Def/ClassDef");
        XML_Load_Defs("Def/AssignDef");
        XML_Load_Defs("Def/TroopsDef");
    }
	
    public string TextLoad(XML_INDEX id, string dicSeach)
    {
        string strTemp;
        xmlLoader.GetInstance().m_XML_Defs[(int)id].TryGetValue(dicSeach, out strTemp);

        return strTemp;
    }

	// Update is called once per frame
    void XML_Load_UnitSpeech()
    {
        Dictionary<string, string> obj;

        TextAsset textAsset = (TextAsset)Resources.Load("XML/UnitSpeech");
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(textAsset.text);

        XmlNodeList xmlList = xmldoc.GetElementsByTagName("Chaos");

        foreach (XmlNode xmlInfo in xmlList)
        {
            XmlNodeList xmlContent = xmlInfo.ChildNodes;
            obj = new Dictionary<string, string>();

            foreach (XmlNode xmlItens in xmlContent) 
            {
                if (xmlItens.Name == "Normal")
                {
                    obj.Add(string.Format("normal_{0}", xmlItens.Attributes["Id"].Value), xmlItens.InnerText);
                }
                else if (xmlItens.Name == "Attack")
                {
                    obj.Add(string.Format("attack_{0}", xmlItens.Attributes["Id"].Value), xmlItens.InnerText);
                }
            }
            m_XML_UnitSpeech.Add(obj);
            int i = 0;
        }
    }

    void XML_Load_ETC(string _fileName)
    {
        Dictionary<string, string> obj;

        TextAsset textAsset = (TextAsset)Resources.Load("XML/" + _fileName);
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(textAsset.text);

        XmlNodeList xmlList = xmldoc.GetElementsByTagName("Object");
        foreach (XmlNode xmlInfo in xmlList)
        {
            XmlNodeList xmlContent = xmlInfo.ChildNodes;
            obj = new Dictionary<string, string>();

            foreach (XmlNode xmlItens in xmlContent)
            {
                obj.Add(xmlItens.Name, xmlItens.InnerText);
            }

            if (_fileName == "Buildings")
                m_XML_Buildings.Add(obj);
            else if (_fileName == "Weapons")
                m_XML_Weapons.Add(obj);
            else if (_fileName == "Events")
                m_XML_Events.Add(obj);
            else if (_fileName == "EventDialog")
                m_XML_EventLogs.Add(obj);
            else
                m_XML_Traits.Add(obj);
        }

    }

    void XML_Load_Defs(string _fileName)
    {
        TextAsset textAsset = (TextAsset)Resources.Load("XML/"+_fileName);
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.LoadXml(textAsset.text);

        XmlNode root = xmldoc.DocumentElement;
        if (root.HasChildNodes)
        {
            Dictionary<string, string> tempDic = new Dictionary<string, string>();

            XmlNodeList child = root.ChildNodes;

            for (int i = 0; i < child.Count; ++i)
            {
                if (child[i].LocalName != "#comment")
                    tempDic.Add(child[i].LocalName, child[i].InnerXml);
            }

            m_XML_Defs.Add(tempDic);
        }
    }
}
